#include <bits/stdc++.h>
#ifndef DEBUG
#define debug
#endif
using namespace std;
using i64 = int64_t;

int main() {
  cin.tie(0)->sync_with_stdio(0);
  int T;
  cin >> T;
  while (T --) {
    int n;
    cin >> n;
    vector<int> a(1 << n);
    for (auto &x : a) cin >> x;
    auto solve = [&](auto &self, int l, int r) {
      if (l == r) {
        cout << a[l] << ' ';
        return;
      }
      int mid = (l + r) / 2;
      int pos = min_element(a.begin() + l, a.begin() + r + 1) - a.begin();
      if (pos > mid) {
        reverse(a.begin() + l, a.begin() + r + 1);
      }
      self(self, l, mid);
      self(self, mid + 1, r);
    };
    solve(solve, 0, (1 << n) - 1);
    cout << endl;
  }
}